#!/usr/local/bin/python

import os
import codecs
import urllib

READER_PATH = '/var/www/vhosts/reader'

def fetch():
	feedcount = 0
	for line in open('%s/feedlist.txt' % READER_PATH).readlines():
		if not line.startswith('#'):
			line = line.strip('\n')
			url = line.split('\t')[-1]
			print 'Fetching', url
			try:
				urlf = urllib.urlopen(url)
				data = urlf.read().decode('utf-8')
				feedcount += 1
				filename = '%s/feeds/feed%s.xml' % (READER_PATH, feedcount)
				feedf = codecs.open(filename , 'w', encoding='utf-8')
				feedf.write(data)
				feedf.close()
				print 'Wrote', filename
			except StandardError:
				print 'Error', url
	print 'Fetching complete.'


if __name__ == '__main__':
	fetch()
